/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.plugins.tbs;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import common.tool.JsonTool;
import es.dmoral.toasty.Toasty;

import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.车辆详情分享微信;

/**
 * 重写WebViewClient
 * Created by JustTong on 2018/12/11.
 */
public class WebCoreClient extends WebViewClient {

    private Activity mActivity;
    private com.tencent.smtt.sdk.WebView mTbsContent;
    private String callbackUrl;

    public WebCoreClient(Activity activity, com.tencent.smtt.sdk.WebView tbsContent) {
        this.mActivity = activity;
        this.mTbsContent = tbsContent;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Logger.i("shouldOverrideUrlLoading>>>" + url);

        Intent intent;
        // 判断URL
        if (url.startsWith("http:") || url.startsWith("https:")) {
            //微信H5支付先决条件
            if (url.startsWith("https://wx.tenpay.com")) {
                Logger.d("callbackUrl=" + callbackUrl);

                Map<String, String> extraHeaders = new HashMap<String, String>();
                String mUrl = callbackUrl;
                extraHeaders.put("Referer", mUrl);
                view.loadUrl(url, extraHeaders);

                //延迟执行
                Timer timer = new Timer();
                TimerTask tast = new TimerTask() {
                    @Override
                    public void run() {
                        mActivity.runOnUiThread(new Runnable() {
                            public void run() {
                                mActivity.finish();
                            }
                        });
                    }
                };
                timer.schedule(tast, 2000);

            } else {
                callbackUrl = url;
                if (url.contains("store-order")) {
                    view.loadUrl(url);
                    EventBus.getDefault().post(new EventBusMsgBean(EventBusMsgBean.EnumStatus.门店订单, ""));
                } else {
                    view.loadUrl(url);
                    EventBus.getDefault().post(new EventBusMsgBean(EventBusMsgBean.EnumStatus.非门店订单, ""));
                }

            }

        }
        // 如下方案可在非微信内部WebView的H5页面中调出微信支付
        if (url.startsWith("weixin://wap/pay?")) {
            try {
                intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                mActivity.startActivity(intent);

            } catch (ActivityNotFoundException e) {
                Toasty.normal(mActivity, "请安装微信最新版！", Toast.LENGTH_LONG).show();
                //ToastUtil.show(mActivity, "请安装微信最新版！");
            }

            return true;
        }
        // ------ 调起支付宝客户端支付  对alipays:相关的scheme处理 -------
        if (url.startsWith("alipays:") || url.startsWith("alipay")) {
            try {
                mActivity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));
            } catch (Exception e) {
                new AlertDialog.Builder(mActivity)
                        .setMessage("未检测到支付宝客户端，请安装后重试。")
                        .setPositiveButton("立即安装", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Uri alipayUrl = Uri.parse("https://d.alipay.com");
                                mActivity.startActivity(new Intent("android.intent.action.VIEW", alipayUrl));
                            }
                        }).setNegativeButton("取消", null).show();
            }
            return true;
        }
        // 调用拨号程序
        if (url.startsWith("tel:")) {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            mActivity.startActivity(intent);

            return true;
        }
        // 发送短信
        if (url.startsWith("sms:")) {
            Uri uri = Uri.parse(url);
            intent = new Intent(Intent.ACTION_SENDTO, uri);
            intent.putExtra("sms_body", "");
            mActivity.startActivity(intent);
        }
        // 返回
        if (url.startsWith("go:")) {
            if (url.startsWith("go:back")) {
                if (mTbsContent.canGoBack()) {
                    mTbsContent.stopLoading();
                    mTbsContent.goBack();
                } else {//go:finish
                    mActivity.finish();
                }
            }
        }

        if (url.startsWith("sharewx:")) {
            shareWX shareWX = JsonTool.parseObject(url.replace("sharewx:", ""), shareWX.class);
            EventBus.getDefault().post(new EventBusMsgBean(车辆详情分享微信, shareWX));
        }
        if (url.startsWith("intention:intention")) {
            mActivity.finish();
        }
        return true;
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        mTbsContent.stopLoading();
        mTbsContent.clearView();

        mTbsContent.setVisibility(View.GONE);
        Log.e("error", "errorCode:" + errorCode + "  description:" + description + "  failingUrl:" + failingUrl);

        int Theme = android.R.style.Theme_DeviceDefault_Light_Dialog_Alert;
        int version = Build.VERSION.SDK_INT;
        if (version > 21) {
            Theme = android.R.style.Theme_DeviceDefault_Light_Dialog_Alert;
        } else {
            Theme = AlertDialog.THEME_DEVICE_DEFAULT_LIGHT;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, Theme);
        builder.setTitle("温馨提示");
        String msg = "";//"检测到状态异常，请联系开发者"+description + "[" + errorCode + "]";
        //if(errorCode==-2){
        msg = "状态异常，请联系管理员或检查网络后再重试！";
        //}
        builder.setMessage(msg);

        builder.setCancelable(false);
        builder.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mActivity.finish();
            }
        });
        builder.show();

    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        // TODO Auto-generated method stub

        Log.d("should", "request.getUrl().toString() is " + request.getUrl().toString());

        return super.shouldInterceptRequest(view, request);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

    }
}
